﻿using System.Collections.Generic;
using System.Linq;

namespace CppUT
{
    namespace Common
    {
        public class TestBuilder
        {
            public TestBuilder(List<string> tests)
            {
                _strTests = tests;
            }

            public Test Build()
            {
                //  Create root test suite
                _strTests.Remove("All Tests");
                var rootTestSuite = new TestSuite("All Tests");

                //  Create test suites
                foreach (var testName in _strTests)
                {
                    if (testName.Contains("::"))
                    {
                        break;
                    }

                    var testSuite = new TestSuite(testName);
                    rootTestSuite.AddTest(testSuite);
                }
                
                //  Add all tests to the respective suites.
                foreach (var testSuite in rootTestSuite.Children)
                {
                    var currentTestSuite = (TestSuite)testSuite;
                    var allTestsInSuite = from test in _strTests
                                          where test.StartsWith(currentTestSuite.Name + "::")
                                          select test;
                    foreach (var test in allTestsInSuite)
                    {
                        currentTestSuite.AddTest(new TestCase(test));
                    }
                }

                return rootTestSuite;
            }

            private readonly List<string> _strTests;
        }
    }
}